/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.avaliation;

import beans.Avaliation;
import beans.Judge;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
import org.xml.sax.SAXException;
import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xml.judge.JudgeReader;

public class AvaliationReader {

    private List<Avaliation> avaliations;
    Judge judge;
    static Logger logger;

    public AvaliationReader(Judge judge) {
        this.judge = judge;
        this.avaliations = new ArrayList<>();
        logger = LoggerFactory.getLogger(AvaliationReader.class);
    }

    public void addAvaliation(Avaliation avaliation) {
        this.avaliations.add(avaliation);
    }

    public List<Avaliation> getAllAvaliationsLoggedUser() {
        List<Avaliation> avaliations = getListAvaliations(judge);
        Collections.sort(avaliations);
        return avaliations;
    }

    public Avaliation getById(Long id) {
        List<Avaliation> allAvaliations = getAllAvaliationsLoggedUser();
        for (Avaliation a : avaliations) {
            if(a.getId().equals(id)){
                return a;
            }
        }
        return null;
    }
    
    public HashMap<Judge, List<Avaliation>> getAllAvaliations(){
        JudgeReader judgeReader = new JudgeReader();
        List<Judge> allJudges = judgeReader.getAllJudges();
        HashMap<Judge, List<Avaliation>> avaliations = new HashMap<>();
        for(Judge j : allJudges){
            avaliations.put(j, getListAvaliations(j));
        }
        return avaliations;
    }
    
    private List<Avaliation> getListAvaliations(Judge judge){
        final String rulesfileName = "src/xml/avaliation/AvaliationRules.xml";
        String datafileName = "src/xml/avaliation/avaliations/" + judge.getName().replaceAll(" ", "_") + ".xml";
        File xmlFile = new File(datafileName);
        if(!xmlFile.exists()){
            return new ArrayList<>();
        }
        Digester d = newLoader(new FromXmlRulesModule() {
            @Override
            protected void loadRules() {
                loadXMLRules(new File(rulesfileName));
            }
        }).newDigester();

        d.push(this);

        try {
            File srcFile = new File(datafileName);
            d.parse(srcFile);
        } catch (IOException ioe) {
            System.out.println("Error reading input file:" + ioe.getMessage());
            logger.error("Error reading input file:" + ioe.getMessage());
        } catch (SAXException se) {
            System.out.println();
            logger.error("Error parsing input file:" + se);
        }

        return avaliations;
    }
}
